package com.weitao.marketserver.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weitao.marketserver.mapper.UserMap;
import com.weitao.marketserver.pojo.ReturnData;
import com.weitao.marketserver.pojo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户相关的控制器
 * 1.登录
 * 2.验证登录
 * 3.注册新用户
 */
@RestController
public class LogCon {

    Logger logger = LoggerFactory.getLogger(LogCon.class);

    @Autowired
    UserMap userMap;

    /**
     * 登录
     * @param phone 用户名
     * @param password 密码
     * @return
     */
    @RequestMapping(value = "/login")
    public ReturnData login(@RequestParam(value = "userphone") String phone, @RequestParam(value = "password") String password) {
        ReturnData result = new ReturnData();

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone", phone);
        queryWrapper.eq("password", password);
        User user = userMap.selectOne(queryWrapper);
        if (user != null) {
            logger.info(phone + password);
            StpUtil.login(phone);
            result.setCode(1);
            result.setMsg("OK");
            result.setDatas(StpUtil.getTokenValue());
        } else {
            result.setCode(2);
            result.setMsg("Erro");
        }
        return result;
    }

    /**
     * 新注册用户
     * @param parentid 推荐人ID
     * @param phone 手机号码
     * @param password 密码
     * @return
     */
    @RequestMapping("/regnew")
    public ReturnData regNew(@RequestParam(value = "recoid") Long parentid, @RequestParam(value = "userphone") String phone, @RequestParam(value = "password") String password) {
        ReturnData returnData = new ReturnData();

        //        校验手机
        if (phone.length() != 11) {
            returnData.setCode(2);
            returnData.setMsg("手机错误");
            return returnData;
        }

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", parentid);

        //校验推荐人
        if (userMap.selectOne(queryWrapper) == null) {
            returnData.setCode(2);
            returnData.setMsg("推荐人错误");
            return returnData;
        }

        //校验是否存在
        queryWrapper.clear();
        queryWrapper.eq("phone", phone);
        if (userMap.selectOne(queryWrapper) != null) {
            returnData.setCode(2);
            returnData.setMsg("此用户已存在");
            return returnData;
        }

        //插入用户
        User user=new User();
        user.setPhone(phone);
        user.setPassword(password);
        userMap.insert(user);
        returnData.setCode(1);
        returnData.setMsg("注册成功");

        return returnData;
    }

    /**
     * 登录判断
     * @return
     */
    @RequestMapping("/islogin")
    public ReturnData isLogin() {
        ReturnData result = new ReturnData();
        if (StpUtil.isLogin()) {
            result.setCode(1);
            result.setMsg("Loged");
        } else {
            result.setCode(2);
            result.setMsg("Not log");
        }

        return result;
    }
}
